package server;

import constants.GameConstants;
import tools.FileoutputUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RandomRewards {
    private static RandomRewards instance;
    private static List<Integer> compiledGold;
    private static List<Integer> compiledJxbox;
    private static List<Integer> compiledSilver;
    private static List<Integer> compiledFishing;
    private static List<Integer> compiledEvent;
    private static List<Integer> compiledEventC;
    private static List<Integer> compiledEventB;
    private static List<Integer> compiledEventA;

    public static RandomRewards getInstance() {
        return RandomRewards.instance;
    }

    protected RandomRewards() {
        System.out.println("[" + FileoutputUtil.CurrentReadable_Time() + "][信息]:初始化随机奖励内存");
        List<Integer> returnArray = (List<Integer>) new ArrayList();
        this.processRewards(returnArray, GameConstants.goldrewards);
        RandomRewards.compiledGold = returnArray;
        returnArray = (List<Integer>) new ArrayList();
        this.processRewards(returnArray, GameConstants.Jxboxrewards);
        RandomRewards.compiledJxbox = returnArray;
        returnArray = (List<Integer>) new ArrayList();
        this.processRewards(returnArray, GameConstants.silverrewards);
        RandomRewards.compiledSilver = returnArray;
        returnArray = (List<Integer>) new ArrayList();
        this.processRewards(returnArray, GameConstants.fishingReward);
        RandomRewards.compiledFishing = returnArray;
        returnArray = (List<Integer>) new ArrayList();
        this.processRewards(returnArray, GameConstants.eventCommonReward);
        RandomRewards.compiledEventC = returnArray;
        returnArray = (List<Integer>) new ArrayList();
        this.processRewards(returnArray, GameConstants.eventUncommonReward);
        RandomRewards.compiledEventB = returnArray;
        returnArray = (List<Integer>) new ArrayList();
        this.processRewards(returnArray, GameConstants.eventRareReward);
        RandomRewards.compiledEventA = returnArray;
        returnArray = (List<Integer>) new ArrayList();
        this.processRewards(returnArray, GameConstants.eventSuperReward);
        RandomRewards.compiledEvent = returnArray;
    }

    private void processRewards(List<Integer> returnArray, int[] list) {
        int lastitem = 0;
        for (int i = 0; i < list.length; ++i) {
            if (i % 2 == 0) {
                lastitem = list[i];
            } else {
                for (int j = 0; j < list[i]; ++j) {
                    returnArray.add(Integer.valueOf(lastitem));
                }
            }
        }
        Collections.shuffle(returnArray);
    }

    public static int getGoldBoxReward() {
        return ((Integer) RandomRewards.compiledGold.get(Randomizer.nextInt(RandomRewards.compiledGold.size()))).intValue();
    }

    public static int getJxBoxReward() {
        return ((Integer) RandomRewards.compiledJxbox.get(Randomizer.nextInt(RandomRewards.compiledJxbox.size()))).intValue();
    }

    public static int getSilverBoxReward() {
        return ((Integer) RandomRewards.compiledSilver.get(Randomizer.nextInt(RandomRewards.compiledSilver.size()))).intValue();
    }

    public static int getFishingReward() {
        return ((Integer) RandomRewards.compiledFishing.get(Randomizer.nextInt(RandomRewards.compiledFishing.size()))).intValue();
    }

    public static int getEventReward() {
        int chance = Randomizer.nextInt(100);
        if (chance < 50) {
            return ((Integer) RandomRewards.compiledEventC.get(Randomizer.nextInt(RandomRewards.compiledEventC.size()))).intValue();
        }
        if (chance < 80) {
            return ((Integer) RandomRewards.compiledEventB.get(Randomizer.nextInt(RandomRewards.compiledEventB.size()))).intValue();
        }
        if (chance < 95) {
            return ((Integer) RandomRewards.compiledEventA.get(Randomizer.nextInt(RandomRewards.compiledEventA.size()))).intValue();
        }
        return ((Integer) RandomRewards.compiledEvent.get(Randomizer.nextInt(RandomRewards.compiledEvent.size()))).intValue();
    }

    static {
        instance = new RandomRewards();
        compiledGold = null;
        compiledJxbox = null;
        compiledSilver = null;
        compiledFishing = null;
        compiledEvent = null;
        compiledEventC = null;
        compiledEventB = null;
        compiledEventA = null;
    }
}
